<template>
    <div>
        <h2>门店信息修改</h2>

        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right"><b>*</b> 店名</td>
                    <td>
                        <input type="text" v-model="queryInfo.ShopName">
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>*</b>营业时间</td>
                    <td>
                        <input type="text" v-model="queryInfo.StartTime">-<input type="text"
                            v-model="queryInfo.EndTime">
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>*</b>服务范围</td>
                    <td>
                        <select v-model="queryInfo.SeverName">
                            <option value="">请选择</option>
                            <option :value="item.Name" v-for="item in bb.list" :key="item.Id">{{ item.Name }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">店主</td>
                    <td>
                        <input type="text" v-model="queryInfo.DName">
                    </td>
                </tr>

                <tr>
                    <td align="right">联系电话</td>
                    <td>
                        <input type="text" v-model="queryInfo.Phone">
                    </td>
                </tr>

                <tr>
                    <td align="right">门店介绍</td>
                    <td>
                        <textarea rows="5" cols="50" v-model="queryInfo.Desc"></textarea>
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>*</b>门店地址</td>
                    <td>
                        <input type="text" v-model="queryInfo.Addr">
                    </td>
                </tr>

                <tr>
                    <td align="right"><b>*</b>状态</td>
                    <td>
                        <input type="radio" name="state" :value="0" v-model="queryInfo.State">启用
                        <input type="radio" name="state" :value="1" v-model="queryInfo.State">禁用
                    </td>
                </tr>

                <tr>
                    <td align="right">门店logo</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="queryInfo.Logo" height="100" width="100" v-if="queryInfo.Logo != ''" alt="">
                    </td>
                </tr>

                <tr>
                    <td align="right">门店图片</td>
                    <td>
                        <input type="file" @change="img1">
                        <img :src="queryInfo.Photo" height="100" width="100" v-if="queryInfo.Photo != ''" alt="">
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="修改" class="btn btn-info" @click="upd">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router'
const router = useRouter();
const route = useRoute();
import moment from 'moment';

onMounted(() => {
    getBang();
    getFan();
})

//---------下拉绑定--------
let bb: any = reactive({
    list: []
})
const getBang = () => {
    axios({
        method: 'get',
        url: '/api/Info/ShowSever'
    })
        .then(res => {
            console.log(res);
            bb.list = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}

//---图片上传---
const img = (e: any) => {
    var obj = e.target.files[0];
    var f = new FormData();
    f.append('file', obj);

    axios({
        method: 'post',
        url: '/api/Info/UpFileImg',
        data: f
    })
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert('图片格式不正确');
                return;
            }
            if (res.data == -2) {
                alert('图片大于2兆');
                return;
            }
            queryInfo.Logo = res.data;

        })
        .catch(err => {
            console.log(err);
        })
}
const img1 = (e: any) => {
    var obj = e.target.files[0];
    var f = new FormData();
    f.append('file', obj);

    axios({
        method: 'post',
        url: '/api/Info/UpFileImg',
        data: f
    })
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert('图片格式不正确');
                return;
            }
            if (res.data == -2) {
                alert('图片大于2兆');
                return;
            }

            queryInfo.Photo = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}

//反填
const getFan = () => {
    axios({
        method: 'get',
        url: '/api/Info/FanTian',
        params: {
            id: route.query.id
        }
    })
        .then(res => {
            console.log(res);
            Object.assign(queryInfo,res.data);
        })
        .catch(err => {
            console.log(err);
        })
}


//--------------修改----------
let queryInfo: any = reactive({
    "Id": route.query.id,
    "ShopName": "",
    "StartTime": "",
    "EndTime": "",
    "SeverName": "",
    "DName": "",
    "Phone": "",
    "Desc": "",
    "Addr": "",
    "State": '',
    "Logo": "",
    "Photo": "",
})

const upd = () => {
    //有效性判断
    if (queryInfo.ShopName == "") {
        alert('店名不能为空');
        return;
    }
    if (queryInfo.StartTime == "") {
        alert('营业时间不能为空');
        return;
    }
    if (queryInfo.EndTime == "") {
        alert('营业时间不能为空');
        return;
    }
    if (queryInfo.SeverName == "") {
        alert('服务范围必选');
        return;
    }
    if (queryInfo.Addr == "") {
        alert('门店地址不能为空');
        return;
    }
    if (queryInfo.State === "") {
        alert('门店状态不能为空');
        return;
    }

    //返回网络请求
    axios({
        method: 'put',
        url: '/api/Info/UpdShop',
        data: queryInfo
    })
        .then(res => {
            console.log(res);
            if (res.data > 0) {
                alert('成功');
                router.push('/shopList')
            }
        })
        .catch(err => {
            console.log(err);
        })
}
</script>

<style scoped>
b {
    color: red;
}
</style>